﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using PloobsEngine.Utils;
using PloobsEngine.Features;
using PloobsEngine.Commands;
using PloobsEngine.Light;
using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine.Input;
using PloobsEngine.Cameras;
using Microsoft.Xna.Framework;
using PloobsEngine.Draw;
using PloobsEngineDemo3;
using PloobsEngine;

namespace Etapa1.Screens
{
    public class ObjectMoverScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;

        public ObjectMoverScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();
            mundo = new BrutalForceWorld(physicWorld);                        
            engine.Game.IsFixedTimeStep = true;
        }
        
        public override void LoadContent(EngineStuff es)
        {            
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            base.LoadContent(es);            

            #region Models

            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(Vector3.Zero, sm);
                CustomDeferred shader = new CustomDeferred(false, false, false, false);
                shader.SpecularIntensity = 0;
                shader.SpecularPower = 0;
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }

            //////OBJECTMOVER TEST
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(-100, 8, 100), 1, 50, new Vector3(5));                
                om = new ObjectMover(pi);
                IShader shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj5 = new SimpleObject(mat, om, sm);
                obj5.Name = "WHITEX";
                mundo.AddObject(obj5);
            }

            #endregion            

            cam = new CameraFirstPerson(true);            
            cam.FarPlane = 3000;

            LightThrowBepu lt = new LightThrowBepu(mundo, engine);         

            #region NormalLight
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            float li = 0.4f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);
            #endregion

            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);
            
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;
            mundo.AddCamera(cam);
            this.World = mundo;


            Picking pk = new Picking();
            engine.AddComponent(pk);

            PickingParameters pp = new PickingParameters(cam, mundo, MouseButtons.RightButton);
            pp.Method = SegmentInterceptMethod.ALL;
            PickingRedirectorCallbackObserver prc = new PickingRedirectorCallbackObserver();
            prc.redirectPicking += new RedirectPicking(prc_redirectPicking);
            pp.Observers.Add(prc);
            CommandProcessor.getCommandProcessor().SendCommandSyncronous(pp);                  
        }

        ObjectMover om = null;
        RayTestInfo ri = null;
        void prc_redirectPicking(object obj)
        {
            ri = obj as RayTestInfo;
            om.GoalVelocity = Vector3.Normalize(ri.ImpactPosition - om.Position) * 10;
            float angle = (float)VectorUtils.FindAngleBetweenTwoVectors2D(om.BepuEntityObject.Entity.InternalLinearVelocity, om.BepuEntityObject.Entity.InternalOrientationMatrix.Forward);
            Matrix rot = om.BepuEntityObject.Entity.InternalOrientationMatrix * Matrix.CreateRotationY(-angle);
            om.GoalOrientation = Quaternion.CreateFromRotationMatrix(rot);
        }
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            Drawing.Draw3dLine(om.Position, om.Position + om.FaceVector * 20,Color.Wheat);
            Drawing.Draw3dLine(om.Position, om.Position + om.Velocity * 20, Color.Red);                        

            if (ri != null)
            {
                Drawing.Draw3dLine(ri.ImpactPosition, ri.ImpactPosition + Vector3.Up * 20, Color.Red);
                Drawing.DrawCircleOrDisk(5, Vector3.Up, ri.ImpactPosition, Color.Purple, 100, true, true);
            }
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }



    }
}

